/*
    Copyright 2011 Wira Mulia, David Fritz

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package plpbotcontrol;

import javax.imageio.ImageIO;
import java.io.InputStream;
import java.awt.image.BufferedImage;
import java.awt.image.BufferStrategy;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JComponent;

import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

import uk.co.caprica.vlcj.test.minimal.MinimalTestPlayer;

/**
 *
 * @author wira
 */
public class VideoFrame extends javax.swing.JFrame {

    //CamCanvas canvas1;
    Canvas canvas1;
    uk.co.caprica.vlcj.player.MediaPlayer vlcPlayer;

    /** Creates new form VideoFrame */
    public VideoFrame() {
        try {

        initComponents();

        //Global.streamConnection = Global.streamLocator.openConnection();
        //Global.streamConnection.setDoInput(true);
        //Global.streamConnection.setUseCaches(false);

        //canvas1 = new CamCanvas(null);
        canvas1 = new Canvas(null);
        canvas1.setSize(new Dimension(640, 480));


        this.add(canvas1);
        //canvas1.createBufferStrategy(2);

        this.setSize(new Dimension(canvas1.getWidth(), canvas1.getHeight()));
        this.pack();

        MediaPlayerFactory factory = new MediaPlayerFactory();

        EmbeddedMediaPlayer mediaPlayer = factory.newMediaPlayer(null);
        mediaPlayer.setVideoSurface(canvas1);

        mediaPlayer.setStandardMediaOptions("http-caching=0");
        mediaPlayer.playMedia("" + Global.streamLocator);
        

        //(new VideoDisplayThread(canvas1)).start();
        //(new GrabberThread()).start();

        } catch(Exception e) {
            System.err.println("video frame exception: " + e);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 660, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}

class GrabberThread extends Thread {
    @Override
    public void run() {
        try {
            
        while(true) {
            //Global.streamFrame = ImageIO.read(Global.streamLocator);
            Thread.sleep(Global.grabberRate);
        }

        }catch (Exception e) {
            Global.streamFrame = null;
            System.err.println("grabber exception: " + e);
            //System.exit(-1);
        }
    }
}
/*
class VideoDisplayThread extends Thread {
    
    CamCanvas canvas1;

    public VideoDisplayThread(CamCanvas canvas1) {
        super();

        this.canvas1 = canvas1;
    }

    @Override
    public void run() {
        try {

        while(true) {
            canvas1.refresh();
            Thread.sleep(Global.painterRate);
        }
            
        } catch(Exception e) {
            System.err.println("painter exception: " + e);
            //System.exit(-1);
        }
    }
}
/*
class CamCanvas extends JComponent {

    private BufferedImage I;
    Graphics2D bufferGraphics;

    public CamCanvas(BufferedImage I) {
        this.I = I;
    }

    public void setImage(BufferedImage I) {
        this.I = I;
    }

    @Override
    public void paint(Graphics g) {
        if(Global.streamFrame != null) {
            BufferedImage frame = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
            bufferGraphics = frame.createGraphics();
            bufferGraphics.drawImage(Global.streamFrame, null, null);
            g.drawImage(frame, 0, 0, null);
            g.setColor(new Color(255, 0, 0));
            g.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 15));
            g.drawString(Global.streamLocator.getHost(), 3, 475);
            g.setColor(new Color(200, 200, 200));
            g.drawRect(315, 238, 10, 4);
            g.drawRect(318, 235, 4, 10);
            g.drawOval(310, 230, 20, 20);
            g.drawOval(256, 176, 128, 128);
            g.drawLine(320, 0, 320, 480);
            g.drawLine(0, 240, 640, 240);
            for(int i = 1; i < 10; i++)
                g.drawLine(i * 64, 230, i * 64, 250);
        } else {
            g.setColor(new Color(255, 0, 0));
            g.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 40));
            g.drawString("NO DATA", 0, 0);
        }
    }

    public void refresh() {
        this.repaint();
    }
}*/

